import { CrudOptions, AddReq, DelReq, EditReq, dict, CrudExpose, UserPageQuery, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import * as api from './api';
import { auth } from "/@/utils/authFunction";
import { request } from '/@/utils/service';

export default function ({ crudExpose }: { crudExpose: CrudExpose }): CreateCrudOptionsRet {
    const pageRequest = async (query: any) => {
        return await api.GetList(query);
    };
    const editRequest = async ({ form, row }: EditReq) => {
        if (row.id) {
            form.id = row.id;
        }
        return await api.UpdateObj(form);
    };
    const delRequest = async ({ row }: DelReq) => {
        return await api.DelObj(row.id);
    };
    const addRequest = async ({ form }: AddReq) => {
        return await api.AddObj(form);
    };

    const exportRequest = async (query: UserPageQuery) => {
        return await api.exportData(query)
    };

    return {
        crudOptions: {
            request: {
                pageRequest,
                addRequest,
                editRequest,
                delRequest,
            },
            actionbar: {
                buttons: {
                    export: {
                        show: auth('SupplierEvaluation:Export'),
                        text: "导出",
                        title: "导出",
                        click() {
                            return exportRequest(crudExpose.getSearchFormData())
                        }
                    },
                    add: {
                        show: auth('SupplierEvaluation:Create'),
                    },
                }
            },
            rowHandle: {
                fixed: 'right',
                width: 200,
                buttons: {
                    view: {
                        type: 'text',
                        order: 1,
                        show: auth('SupplierEvaluation:Retrieve')
                    },
                    edit: {
                        type: 'text',
                        order: 2,
                        show: auth('SupplierEvaluation:Update')
                    },
                    copy: {
                        type: 'text',
                        order: 3,
                        show: auth('SupplierEvaluation:Copy')
                    },
                    remove: {
                        type: 'text',
                        order: 4,
                        show: auth('SupplierEvaluation:Delete')
                    },
                },
            },
            columns: {
                supplier: {
                    title: '供应商',
                    type: 'dict-select',
                    search: { show: true },
                    dict: dict({
                        url: '/api/supplier/',
                        value: 'id',
                        label: 'name'
                    }),
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '供应商必选' }],
                        component: {
                            placeholder: '请选择供应商',
                        },
                    },
                },
                evaluation_date: {
                    title: '评估日期',
                    type: 'date',
                    search: { show: true },
                    column: {
                        width: 120,
                        component: { name: 'fs-date-format', format: 'YYYY-MM-DD' }
                    },
                    form: {
                        rules: [{ required: true, message: '评估日期必填' }],
                        component: {
                            format: 'YYYY-MM-DD',
                            valueFormat: 'YYYY-MM-DD',
                            placeholder: '请选择评估日期'
                        }
                    }
                },
                quality_score: {
                    title: '质量评分',
                    type: 'number',
                    search: { show: false },
                    column: {
                        width: 180,
                        component: {
                            name: 'el-rate',
                            props: {
                                max: 5,
                                allowHalf: true,
                                disabled: true,
                                texts: ['极差', '失望', '一般', '满意', '惊喜'],
                                showText: true,
                                textColor: '#ff9900'
                            }
                        }
                    },
                    form: {
                        rules: [{ required: true, message: '质量评分必填' }],
                        component: {
                            name: 'el-rate',
                            max: 5,
                            allowHalf: true,
                            texts: ['极差', '失望', '一般', '满意', '惊喜'],
                            showText: true,
                            textColor: '#ff9900'
                        }
                    },
                },
                delivery_score: {
                    title: '交付评分',
                    type: 'number',
                    search: { show: false },
                    column: {
                        width: 180,
                        component: {
                            name: 'el-rate',
                            props: {
                                max: 5,
                                allowHalf: true,
                                disabled: true,
                                texts: ['极差', '失望', '一般', '满意', '惊喜'],
                                showText: true,
                                textColor: '#ff9900'
                            }
                        }
                    },
                    form: {
                        rules: [{ required: true, message: '交付评分必填' }],
                        component: {
                            name: 'el-rate',
                            max: 5,
                            allowHalf: true,
                            texts: ['极差', '失望', '一般', '满意', '惊喜'],
                            showText: true,
                            textColor: '#ff9900'
                        }
                    },
                },
                price_score: {
                    title: '价格评分',
                    type: 'number',
                    search: { show: false },
                    column: {
                        width: 180,
                        component: {
                            name: 'el-rate',
                            props: {
                                max: 5,
                                allowHalf: true,
                                disabled: true,
                                texts: ['极差', '失望', '一般', '满意', '惊喜'],
                                showText: true,
                                textColor: '#ff9900'
                            }
                        }
                    },
                    form: {
                        rules: [{ required: true, message: '价格评分必填' }],
                        component: {
                            name: 'el-rate',
                            max: 5,
                            allowHalf: true,
                            texts: ['极差', '失望', '一般', '满意', '惊喜'],
                            showText: true,
                            textColor: '#ff9900'
                        }
                    },
                },
                service_score: {
                    title: '服务评分',
                    type: 'number',
                    search: { show: false },
                    column: {
                        width: 180,
                        component: {
                            name: 'el-rate',
                            props: {
                                max: 5,
                                allowHalf: true,
                                disabled: true,
                                texts: ['极差', '失望', '一般', '满意', '惊喜'],
                                showText: true,
                                textColor: '#ff9900'
                            }
                        }
                    },
                    form: {
                        rules: [{ required: true, message: '服务评分必填' }],
                        component: {
                            name: 'el-rate',
                            max: 5,
                            allowHalf: true,
                            texts: ['极差', '失望', '一般', '满意', '惊喜'],
                            showText: true,
                            textColor: '#ff9900'
                        }
                    },
                },
                total_score: {
                    title: '总评分',
                    type: 'number',
                    search: { show: false },
                    column: {
                        width: 180,
                        component: {
                            name: 'el-rate',
                            props: {
                                max: 5,
                                allowHalf: true,
                                disabled: true,
                                texts: ['极差', '失望', '一般', '满意', '惊喜'],
                                showText: true,
                                textColor: '#ff9900'
                            }
                        }
                    },
                    form: {
                        rules: [{ required: true, message: '总评分必填' }],
                        component: {
                            name: 'el-rate',
                            max: 5,
                            allowHalf: true,
                            texts: ['极差', '失望', '一般', '满意', '惊喜'],
                            showText: true,
                            textColor: '#ff9900'
                        }
                    },
                },
                evaluator: {
                    title: '评估人',
                    type: 'input',
                    search: { show: true },
                    column: {
                        width: 100,
                    },
                    form: {
                        rules: [{ required: true, message: '评估人必填' }],
                        component: {
                            placeholder: '请输入评估人',
                        },
                    },
                },
                evaluation_content: {
                    title: '评估内容',
                    type: 'textarea',
                    search: { show: true },
                    column: {
                        minWidth: 200,
                    },
                    form: {
                        rules: [{ required: true, message: '评估内容必填' }],
                        component: {
                            placeholder: '请输入评估内容',
                            span: 24,
                        },
                    },
                },
            },
        },
    };
} 